//
//  ZHLineChartView.m
//  Doctor
//
//  Created by liuzhishan on 15/12/16.
//  Copyright © 2015年 shanshan. All rights reserved.
//

#import "ZHLineChartView.h"
#import "ZHLineCollectionViewCell.h"
#import "ZHClinicAllData.h"
#import "ZHAlertView.h"
#define  HIGHTCOUNT  5
#define  HEIGHTGAP   60

#define CIRCLERADIUS   10.5
#define CIRCLEBIGRADIUS   16

#define  IMAGEBGNAME  @"LineBg"

@interface ZHLineChartView ()<UICollectionViewDataSource,UICollectionViewDelegate,clinicShowDele>

@property (nonatomic,retain) UICollectionView *myColletionView;

@property (nonatomic,retain) ZHAlertView *alertView;

@property (nonatomic,strong) UIImageView  *nowImageview;

@property (nonatomic,strong) UIImageView  *anotherImageview;

@property (nonatomic,assign) CGFloat nowCenterY;

@property (nonatomic,assign) CGFloat otherCenterY;

@property (nonatomic,strong) UIView *hideView;

@end

@implementation ZHLineChartView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
       
        [self addMyChildViews];
        
    }
    return self;
}


-(void)showClinicMessage:(NSString *)meg andIndex:(NSInteger)index andDirection:(NSString *)direction andColorName:(NSString *)name andNowImageView:(UIImageView*)nowImageview
{
    
    
    self.nowImageview = nowImageview;
    
    ZHLineCollectionViewCell *anotherCell = (ZHLineCollectionViewCell*)[self.myColletionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index +1 inSection:0]];
    
    if ([name isEqualToString:@"blue"]) {
        
       self.anotherImageview =   anotherCell.circleLeftImage;
    }
    
    else
        
    {
        self.anotherImageview =   anotherCell.circleLeftOrangeImage;
    }
    
    if ([direction isEqualToString:@"left"]) {
        
        anotherCell = (ZHLineCollectionViewCell*)[self.myColletionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index -1  inSection:0]];
        
        if ([name isEqualToString:@"blue"]) {
            
            self.anotherImageview =   anotherCell.circleRightImage;
        }
        
        else
            
        {
            self.anotherImageview =   anotherCell.circleRightOrangeImage;
        }

        
        
    }
    
    self.nowCenterY = self.nowImageview.centerY;
    
    self.otherCenterY = self.anotherImageview.centerY;
    
    
    
    [self changeImageviewScaleAndImage];
    
    
    
    
   if(self.alertView == nil)
   {
        self.alertView = [[ZHAlertView alloc]init];
        self.alertView.layer.masksToBounds = YES;
        self.alertView.layer.cornerRadius =  10*WIDPro;
       
       [self.alertView setMyFramewithX:0 andY:0 andWidth:self.width/3 andHeight:self.height/4];
       
       [self addSubview:self.alertView];
       self.alertView.centerX = self.width/2;
       self.alertView.centerY = self.height/3;

       
   }
       [self.alertView setAlertMessage:meg andUIFond:20 andAlpha:0.5];
    
    
    self.hideView.hidden= NO;
    
}

-(void)changeImageviewScaleAndImage
{
    self.nowImageview.width = CIRCLEBIGRADIUS*WIDPro;
    self.anotherImageview.width = CIRCLEBIGRADIUS *WIDPro;
    
    self.nowImageview.height = CIRCLEBIGRADIUS * 2 *WIDPro;
    self.anotherImageview.height = CIRCLEBIGRADIUS * 2*WIDPro;
    
    if (self.nowImageview.x != 0) {
        
         self.nowImageview.x = 78 *WIDPro -  self.nowImageview.width;
    }
   
    
    if (self.anotherImageview.x != 0) {
        
         self.anotherImageview.x = 78 *WIDPro -  self.anotherImageview.width;
    }
    
    self.nowImageview.centerY = self.nowCenterY;
    self.anotherImageview.centerY = self.otherCenterY;
    
    [self changeImageWithTag:self.nowImageview andName:@"high"];
    [self changeImageWithTag:self.anotherImageview andName:@"high"];

    
   
}


-(void)changeImageWithTag:(UIImageView*)imageView andName:(NSString *)imageName
{
    
    
    
    if (imageView.tag == 10001) {
        
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@blueCircleLeftImage",imageName]];
    }
    
    else if (imageView.tag == 10002) {
    
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@blueCircleRightImage",imageName]];
        
        
    }
    
    else if (imageView.tag == 10003) {
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@orangeCircleLeftImage",imageName]];

        
        
    }
    
    else if (imageView.tag == 10004) {
        
       imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@orangeCircleRightImage",imageName]];
    }
    

}

-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self addMYLable];
    
    [self.myColletionView reloadData];
    
}

#pragma mark 添加子视图

-(void)addMyChildViews
{
    [self addMYImageView];
    [self addMyLables];
    [self addMYCollection];
    
    [self addMyHideView];
    
    
}
-(void)addMyHideView
{
    _hideView = [[UIView alloc]init];
    
    _hideView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self addSubview:_hideView];

    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeLeft) multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeRight) multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeTop) multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:_hideView attribute:(NSLayoutAttributeBottom) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeBottom) multiplier:1.0 constant:0]];
    
    
    _hideView.backgroundColor = [UIColor clearColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHideView)];
    [_hideView addGestureRecognizer:tap];
    
    _hideView.hidden = YES;
    
   }

-(void)clickHideView
{
    [self changeImageWithTag:self.nowImageview andName:@""];
    [self changeImageWithTag:self.anotherImageview andName:@""];
    
    
    self.nowImageview.width = CIRCLERADIUS*WIDPro;
    self.anotherImageview.width = CIRCLERADIUS *WIDPro;
    
    self.nowImageview.height = CIRCLERADIUS *2*WIDPro;
    self.anotherImageview.height = CIRCLERADIUS * 2*WIDPro;
    
    if (self.nowImageview.x != 0) {
        
        self.nowImageview.x = 78 *WIDPro -  self.nowImageview.width;
    }
    
    
    if (self.anotherImageview.x != 0) {
        
        self.anotherImageview.x = 78 *WIDPro -  self.anotherImageview.width;
    }
    
    self.nowImageview.centerY = self.nowCenterY;
    self.anotherImageview.centerY = self.otherCenterY;

    [self.alertView removeFromSuperview];
    self.alertView = nil;
    
    self.hideView.hidden = YES;
    
    
}



-(void)addMyLables
{
    
    for ( NSInteger i = 0 ;  i < HIGHTCOUNT ; i ++) {
        
        UILabel *lable = [[UILabel alloc]init];
        
        lable.textColor = [UIColor whiteColor];
        
        lable.textAlignment = NSTextAlignmentRight;
        lable.font = [UIFont systemFontOfSize:16 * WIDPro -2];
        
        [self addSubview:lable];
        
        lable.tag = 1000 + i;
        
        
    }

    
}
-(void)addMYImageView
{
    UIImageView *imageView = [[UIImageView alloc]init];
    
    imageView.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self addSubview:imageView];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeLeft) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeLeft) multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeRight) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeRight) multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeTop) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeTop) multiplier:1.0 constant:0]];
    
    [self addConstraint:[NSLayoutConstraint constraintWithItem:imageView attribute:(NSLayoutAttributeBottom) relatedBy:(NSLayoutRelationEqual) toItem:self attribute:(NSLayoutAttributeBottom) multiplier:1.0 constant:0]];
 
    imageView.image = [UIImage imageNamed:IMAGEBGNAME];
    
}

#pragma mark 修改Lable 值
-(void)addMYLable
{
    
    ZHSaveCommenMeg *meg = [ZHSaveClickDate shareData].judgeModel;

    CGFloat oneNubValue = meg.max / (HIGHTCOUNT - 1);
//     if (meg.max >= 4)
//     {
//         oneNubValue = (CGFloat)(NSInteger)(oneNubValue + 1);
//     }
    CGFloat currentNubValue = 0;
    
    for ( NSInteger i = 0 ;  i < HIGHTCOUNT ; i ++) {
        
        UILabel *lable =  (UILabel*)[self viewWithTag:(1000+i)];
        
        
        currentNubValue = meg.max - i*oneNubValue;
      //  currentNubValue =  (HIGHTCOUNT -  i)*oneNubValue;
       
        
        lable.text = [NSString stringWithFormat:@"%.2f",currentNubValue];
        
        if (meg.max >= 4) {
            
             lable.text = [NSString stringWithFormat:@"%.f",currentNubValue];
        }
       
        
        [lable setMyFramewithX:10 andY:20 + (i * HEIGHTGAP) andWidth:36 andHeight:20];
        
        
    }
    
}
#pragma  mark  添加Collection

-(void)addMYCollection
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    flowLayout.itemSize = CGSizeMake(WIDPro  * 78 , HEIPro *  270);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.sectionInset = UIEdgeInsetsMake(20*HEIPro,0, 0, 0);
    
    _myColletionView = [[UICollectionView alloc] initWithFrame: CGRectMake(50 *WIDPro,8*HEIPro, 859*WIDPro, HEIPro*280)collectionViewLayout:flowLayout];
    _myColletionView.backgroundColor = [UIColor clearColor];
    _myColletionView.bounces = NO;
    _myColletionView.delegate = self;
    _myColletionView.dataSource = self;

    
    
    [_myColletionView registerClass:[ZHLineCollectionViewCell class] forCellWithReuseIdentifier:@"ClinicLineCell"];
    
    
    [self addSubview:_myColletionView];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if(self.dataArray == nil)return 0;
        
    return self.dataArray.count + 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZHLineCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClinicLineCell" forIndexPath:indexPath];
    cell.clinicDele = self;
    
    [cell setDataWithArray:self.dataArray andIndex:indexPath.row];
    
    
    return cell;
}


@end
